package com.example.demo.Controller;


import com.example.demo.Entity.Warehouse;
import com.example.demo.Service.WarehouseService;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/warehouse")
@CrossOrigin(origins = "*")//允许所有同源访问，防止出现跨域问题
public class WarehouseController {

    private final WarehouseService warehouseService;

    @Autowired
    public WarehouseController(WarehouseService warehouseService) {
        this.warehouseService = warehouseService;
    }

    @PostMapping
    public Warehouse createWarehouse(@RequestBody Warehouse warehouse) {
        return warehouseService.createWarehouse(warehouse);
    }

    @PostMapping("/searchWarehouseByName")//获取该仓库的仓库信息
    public Map<String, Object> searchWarehouseByName(@RequestBody Warehouse warehouse){
        System.out.println(warehouse.getName());
        return warehouseService.searchWarehouseByName(warehouse.getName());
    }

    @PostMapping("/getAllWarehouses")//获取该仓库的仓库信息
    public Map<String, Object> findAllWarehouse(){

        return warehouseService.findAllWarehouse();
    }

    @PostMapping("/update")
    public Map<String, Object> updateWarehouseByName(@RequestBody Warehouse warehouse){
        System.out.println(warehouse.getName());
        return warehouseService.updateWarehouseByName(warehouse);
    }

    @PostMapping("/allWarehouseList")
    public Map<String, Object> getAllWarehouseList(){
        System.out.println("allWarehouseList：请求成功");
        return warehouseService.getAllWarehouseList();
    }
    
    @GetMapping("/{id}")
    public Optional<Warehouse> getWarehouseById(@PathVariable Long id) {
        return warehouseService.getWarehouseById(id);
    }

    @GetMapping
    public List<Warehouse> getAllWarehouses() {
        return warehouseService.getAllWarehouses();
    }

    @PutMapping("/{id}")
    public Warehouse updateWarehouse(@PathVariable Long id, @RequestBody Warehouse warehouse) {
        return warehouseService.updateWarehouse(id, warehouse);
    }

    @DeleteMapping("/{id}")
    public boolean deleteWarehouse(@PathVariable Long id) {
        return warehouseService.deleteWarehouse(id);
    }
}

